package com.cn.hnust.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.cn.hnust.pojo.Tavern;
import com.cn.hnust.service.ITavernService;
import com.cn.hnust.util.Page;
import com.cn.hnust.util.Qiniu;

@Controller
@RequestMapping("tavern")
public class TavernController {
	@Autowired
	private ITavernService TavernService;

	@RequestMapping(value = "/List")
	public String Columnlist(Model model, String pageNow) {
		Page page = null;// new 对象在下面
		int totalCount = TavernService.getCount();// 获取当前用户总记录条数
		int pageNow1 = Integer.parseInt(pageNow);
		page = new Page(totalCount, pageNow1);// 这样写的好处，判断完成外面可以继续调用
		model.addAttribute("page", page);
		List<Tavern> tavern = TavernService.queryAllRecord(page);
		model.addAttribute("page", page);
		model.addAttribute("tavern", tavern);
		return "tavern";
	}

	@RequestMapping(value = "/del")
	@ResponseBody
	public Map<String, Object> DelColumn(int tavern_id, Model model) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if (tavern_id > 0) {
			TavernService.delTavern(tavern_id);
			int pageNow = 1;
			Page page = null;
			int totalCount = TavernService.getCount();
			page = new Page(totalCount, pageNow);
			List<Tavern> tavern = TavernService.queryAllRecord(page);
			model.addAttribute("page", page);
			model.addAttribute("tavern", tavern);
			resultMap.put("result", "删除成功");
			return resultMap;
		}

		return null;
	}

	@RequestMapping(value = "/add")
	public String TavernAdd() {

		return "TavernAdd";
	}

	@RequestMapping(value = "/TavernAdd")
	public String ColumnAdd(Model model, HttpServletRequest request, String pageNow,
			@RequestParam("pic") MultipartFile pic) {
		String tavern_name = request.getParameter("tavern_name");
		String tavern_price = request.getParameter("tavern_price");

		String tavern_path = request.getParameter("tavern_path");
		String tavern_describe = request.getParameter("tavern_describe");
		String column_tavern = request.getParameter("column_tavern");
		if (!StringUtils.isEmpty(pic)) {
			String path = "c:\\image\\";
			String originalFileName = pic.getOriginalFilename();
			// 新的图片名称
			String newFileName = UUID.randomUUID() + originalFileName.substring(originalFileName.lastIndexOf("."));
			// 新的图片
			String name = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS").format(new Date());
			System.out.println(newFileName);
			String extension = FilenameUtils.getExtension(originalFileName);
			File newFile = new File(path + newFileName);
			String updz = path + newFileName;
			// 将内存中的数据写入磁盘

			try {
				pic.transferTo(newFile);
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			String pho = null;

			Qiniu qiniuUtils = new Qiniu();
			try {
				pho = qiniuUtils.upload(newFileName, updz);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			Tavern taver = new Tavern();
			Page page = null;
			int pageNow1 = Integer.parseInt(pageNow);
			int totalCount = TavernService.getCount();
			page = new Page(totalCount, pageNow1);
			taver.setTavern_name(tavern_name);
			taver.setTavern_price(tavern_price);
			taver.setTavern_path(tavern_path);
			taver.setTavern_prcture(pho);
			taver.setTavern_describe(tavern_describe);
			taver.setColumn_tavern(column_tavern);
			TavernService.insert(taver);
			List<Tavern> tavern = TavernService.queryAllRecord(page);
			model.addAttribute("page", page);
			model.addAttribute("tavern", tavern);
			return "tavern";

		}
		return null;
	}

	@RequestMapping(value = "/update")
	public String UpColumn(int tavern_id, Model model) {
		if (tavern_id > 0) {
			Tavern tavern = TavernService.getUser(tavern_id);
			model.addAttribute("tavern", tavern);
			return "TavernUp";
		}
		return null;
	}

	@RequestMapping(value = "/TavernUp")
	public String TavernUp(Model model, String pageNow, HttpServletRequest request, int tavern_id,
			@RequestParam("pic") MultipartFile pic) {
		String tavern_name = request.getParameter("tavern_name");
		String tavern_price = request.getParameter("tavern_price");

		String tavern_path = request.getParameter("tavern_path");
		String tavern_describe = request.getParameter("tavern_describe");
		String column_tavern = request.getParameter("column_tavern");
		if (!StringUtils.isEmpty(pic)) {
			String path = "c:\\image\\";
			String originalFileName = pic.getOriginalFilename();
			// 新的图片名称
			String newFileName = UUID.randomUUID() + originalFileName.substring(originalFileName.lastIndexOf("."));
			// 新的图片
			String name = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS").format(new Date());
			System.out.println(newFileName);
			String extension = FilenameUtils.getExtension(originalFileName);
			File newFile = new File(path + newFileName);
			String updz = path + newFileName;
			// 将内存中的数据写入磁盘

			try {
				pic.transferTo(newFile);
			} catch (IllegalStateException e) {

				e.printStackTrace();
			} catch (IOException e) {

				e.printStackTrace();
			}

			String pho = null;

			Qiniu qiniuUtils = new Qiniu();
			try {
				pho = qiniuUtils.upload(newFileName, updz);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			Tavern taver = new Tavern();
			Page page = null;
			int pageNow1 = Integer.parseInt(pageNow);
			int totalCount = TavernService.getCount();
			page = new Page(totalCount, pageNow1);
			taver.setTavern_name(tavern_name);
			taver.setTavern_id(tavern_id);
			taver.setTavern_price(tavern_price);
			taver.setTavern_path(tavern_path);
			taver.setTavern_prcture(pho);
			taver.setTavern_describe(tavern_describe);
			taver.setColumn_tavern(column_tavern);
			TavernService.updateByPrimaryKey(taver);
			List<Tavern> tavern = TavernService.queryAllRecord(page);
			model.addAttribute("page", page);
			model.addAttribute("tavern", tavern);
			return "tavern";

		}
		return null;
	}
}
